import React, { Component } from "react";
import store from "../store";

class ClassManualTest extends Component {
  constructor(props) {
    super(props);
    this.state = store.getState(); // 初始化 state
  }

  componentDidMount() {
    // 订阅 store
    this.unsubscribe = store.subscribe(() => {
      this.setState(store.getState()); // 更新组件 state
    });
  }

  componentWillUnmount() {
    // 卸载时取消订阅
    this.unsubscribe();
  }

  render() {
    return (
      <div>
        <h2>类组件 + 手动订阅 store</h2>
        <p>当前值: {this.state.value}</p>
        <button onClick={() => store.dispatch({ type: "counter/incremented" })}>+1</button>
        <button onClick={() => store.dispatch({ type: "counter/decremented" })}>-1</button>
      </div>
    );
  }
}

export default ClassManualTest;
